/**
覆盖trigger：Reagent,Product,Equipment,Creditchange,WorkOrder
覆盖class:Cbl_SumModel,Cbl_ProductAuto,Cbl_AutoAsset,CreditchangeUpdateAccount,Cbl_WorkOrderProductToAssert

 */
@isTest(SeeAllData=true)
private class Cbl_AutoAsset_Test {
    
    static testMethod void testMethod2(){

         Id ldcProfile = [Select Id From Profile Where Name like '%工程师%' limit  1  ].Id;

         User u1 = new User( Alias = 'samLdc1', 
                         Email = 'ldc@test.com.maike',
                         Emailencodingkey = 'UTF-8', 
                         Lastname = '用户1', 
                         Languagelocalekey = 'en_US',
                         Localesidkey = 'en_US', 
                         Profileid = ldcProfile, 
                         Country = 'United States',
                         IsActive = true,
                         CompanyName= 'Y-MKSY',
                         SubCompany__c='四川省迈克实业有限公司',
                         UserDepartment__c='客户服务中心(工程部)',
                         Timezonesidkey = 'America/Los_Angeles', 
                         EmployeeNumber = '123451',
                         Username = 'Matldc1@test.com.maike',
                         FederationIdentifier ='123451', 
                         ResponseCode__c = 'TTTT' );
         insert u1;
         /*Contact con = TestUtils.createContact();
         con.Post__c = '科长/主任';
         insert con ;*/

        //产品结构
        ProductStructure__c ps1=new ProductStructure__c();
        ps1.Name='仪器';
        ps1.Classify1__c='生化';
        ps1.Classify2__c='生化-设备';
        ps1.Classify3__c='仪器';
        insert ps1;
     
        
          Brand__c brand = new Brand__c(
            BrandShortName__c = '四川迈克'
        );
        insert brand;
        //产品
        Product__c p1=TestUtils.createProduct();
        p1.Name='仪器';
        p1.Category0__c = '仪器';
        p1.Model__c='Model1228';
        p1.ProductStructure__c=ps1.Id;
        p1.CRMBrand__c = brand.Id;
        p1.RecordTypeId =  Utils.getRecordTypeIdByDevNameAndObjName('NCProduct','Product__c');
        insert p1;
        
      
        
        
            

        Province__c provice =  TestUtils.createProvince();
        insert provice;

        Account acc=new Account();
        acc.Name='Test1229J';
        acc.Level__c = '一级医院';
        acc.Province__c = provice.Id;
        insert acc;
        Dealer__c d = new Dealer__c(
            Only__c='Dealer__c'
        );
        insert d;

        Equipment__c eq=new Equipment__c();
        //eq.Name='testYQ';
        eq.Account__c=acc.Id;
        eq.Product__c=p1.Id;
        eq.IMEINumber__c = 'x0003';
        eq.type__c ='退机';

        //update  eq;
        insert eq;
        System.debug('++++++++++');

        InstallationApply__c ins = new InstallationApply__c();
        ins.InstallationType__c='新装机';
        //必填字段 chatter 通知工作流引用到的字段都为必填字段
        ins.Instrument__c = p1.Id;
        ins.NEWDealer__c = d.Id;
        ins.Hospital__c = acc.Id;
        ins.InstallEstiDate__c = Date.newInstance(2017, 12, 1);
        ins.InstallsNumber__c = 1;
        ins.EffectiveTime__c = System.now();
        ins.WhetherStandalone__c = '是';
        ins.ReturnedMachine__c = eq.Id;
        insert ins;


        Case cas=new Case();
        cas.AccountId=acc.Id;
        cas.InstallationApply__c = ins.Id;
        cas.Engineer__c = u1.Id;
        cas.User__c = u1.Id;
        cas.Manufacturer__c = p1.Id;
        cas.IMEINumber__c = 'x0004';

        cas.ContactId = [select Id from Contact where AccountId =: acc.Id limit 1 ].Id;
        insert cas;

        


        WorkOrder__c wo=new WorkOrder__c();
        wo.Case__c=cas.Id;
        wo.Account__c=acc.Id;
        wo.Status__c='处理中';
        wo.ManufactureInfo__c=p1.Id;
        wo.IMEINumber__c = 'x0004';
        wo.recordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('InstallationOrder','WorkOrder__c');
        wo.WhetherToMakeASingle__c = '否';

        insert wo;

         wo.Status__c='完成';
        update wo;
        System.debug('------>');
        
         Case cas1=new Case();
        cas1.AccountId=acc.Id;
        cas1.InstallationApply__c = ins.Id;
        cas1.Engineer__c = u1.Id;
        cas1.User__c = u1.Id;
        cas1.Manufacturer__c = p1.Id;
        cas1.IMEINumber__c = 'x0005';

        cas1.ContactId = [select Id from Contact where AccountId =: acc.Id limit 1 ].Id;
        insert cas1;
        System.Test.startTest();


        WorkOrder__c wo1=new WorkOrder__c();
        wo1.Case__c=cas1.Id;
        wo1.Account__c=acc.Id;
        wo1.Status__c= '处理中';
        wo1.IMEINumber__c = 'x0005';
        wo1.ManufactureInfo__c=p1.Id;
        wo1.recordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('NonAgentInstallationOrder','WorkOrder__c');
        insert wo1;

        wo1.Status__c = '完成';
        update wo1;
        



       

      

    }
    static testMethod void myUnitTest() {
        Id ldcProfile = [Select Id From Profile Where Name like '%工程师%' limit  1  ].Id;

         User u1 = new User( Alias = 'samLdc1', 
                         Email = 'ldc@test.com.maike',
                         Emailencodingkey = 'UTF-8', 
                         Lastname = '用户1', 
                         Languagelocalekey = 'en_US',
                         Localesidkey = 'en_US', 
                         Profileid = ldcProfile, 
                         Country = 'United States',
                         IsActive = true,
                         CompanyName= 'Y-MKSY',
                         SubCompany__c='四川省迈克实业有限公司',
                         UserDepartment__c='客户服务中心(工程部)',
                         Timezonesidkey = 'America/Los_Angeles', 
                         EmployeeNumber = '123451',
                         Username = 'Matldc1@test.com.maike',
                         FederationIdentifier ='123451', 
                         ResponseCode__c = 'TTTT' );
         insert u1;
         /*Contact con = TestUtils.createContact();
         con.Post__c = '科长/主任';
         insert con ;*/

        //产品结构
        ProductStructure__c ps1=new ProductStructure__c();
        ps1.Name='仪器';
        ps1.Classify1__c='生化';
        ps1.Classify2__c='生化-设备';
        ps1.Classify3__c='仪器';
        insert ps1;
        
        ProductStructure__c ps2=new ProductStructure__c();
        ps2.Name='试剂';
        ps2.Classify1__c='生化';
        ps2.Classify2__c='生化-试剂';
        ps2.Classify3__c='试剂';
        insert ps2;
        
        ProductStructure__c ps3=new ProductStructure__c();
        ps3.Name='耗材';
        ps3.Classify1__c='辅助器械';
        ps3.Classify2__c='生化-设备';
        ps3.Classify3__c='耗材';
        insert ps3;
         
        ProductStructure__c ps4=new ProductStructure__c();
        ps4.Name='配件';
        ps4.Classify1__c='生化';
        ps4.Classify2__c='生化-设备';
        ps4.Classify3__c='配件';
        insert ps4;
        
          Brand__c brand = new Brand__c(
        	BrandShortName__c = '四川迈克'
        );
        insert brand;
        //产品
        Product__c p1=TestUtils.createProduct();
        p1.Name='仪器';
        p1.Category0__c = '仪器';
        p1.Model__c='Model1228';
        p1.ProductStructure__c=ps1.Id;
        p1.CRMBrand__c = brand.Id;
        p1.RecordTypeId =  Utils.getRecordTypeIdByDevNameAndObjName('NCProduct','Product__c');
        insert p1;
        
      
        
        Product__c p2=TestUtils.createProduct();
        p2.Name='试剂';
        p2.Model__c='Model1228';        
        p2.ProductStructure__c=ps2.Id;
        insert p2;
        
        Product__c p3=TestUtils.createProduct();
        p3.Name='耗材';
        p3.ProductStructure__c=ps3.Id;
        p3.Model__c='Model1228';
        p3.RecordTypeId =  Utils.getRecordTypeIdByDevNameAndObjName('NCProduct','Product__c');
        insert p3;
        
        Product__c p4=TestUtils.createProduct();
        p4.Name='配件';
        p4.ProductStructure__c=ps4.Id;
        p4.Model__c='Model1228';
        p4.RecordTypeId =  Utils.getRecordTypeIdByDevNameAndObjName('NCProduct','Product__c');
        p4.NCCode__c = 'pj00001';
        insert p4;
            

        Province__c provice =  TestUtils.createProvince();
        insert provice;

        Account acc=new Account();
        acc.Name='Test1229J';
        acc.Level__c = '一级医院';
        acc.Province__c = provice.Id;
        insert acc;
        Dealer__c d = new Dealer__c(
        	Only__c='Dealer__c'
        );
        insert d;





        InstallationApply__c ins = new InstallationApply__c();
        ins.InstallationType__c='新装机';
        //必填字段 chatter 通知工作流引用到的字段都为必填字段
        ins.Instrument__c = p1.Id;
        ins.NEWDealer__c = d.Id;
        ins.Hospital__c = acc.Id;
        ins.InstallEstiDate__c = Date.newInstance(2017, 12, 1);
        ins.InstallsNumber__c = 1;
        ins.EffectiveTime__c = System.now();
        ins.WhetherStandalone__c = '是';
        insert ins;


        Case cas=new Case();
        cas.AccountId=acc.Id;
        cas.InstallationApply__c = ins.Id;
        cas.Engineer__c = u1.Id;
        cas.User__c = u1.Id;
        cas.Manufacturer__c = p1.Id;
        cas.IMEINumber__c = 'xxxxxx1';

        cas.ContactId = [select Id from Contact where AccountId =: acc.Id limit 1 ].Id;
        insert cas;

        


        WorkOrder__c wo=new WorkOrder__c();
        wo.Case__c=cas.Id;
        wo.Account__c=acc.Id;
        wo.Status__c='处理中';
        wo.ManufactureInfo__c=p1.Id;
        wo.recordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('InstallationOrder','WorkOrder__c');
        wo.WhetherToMakeASingle__c = '否';

        insert wo;
        
         Case cas1=new Case();
        cas1.AccountId=acc.Id;
        cas1.InstallationApply__c = ins.Id;
        cas1.Engineer__c = u1.Id;
        cas1.User__c = u1.Id;
        cas1.Manufacturer__c = p1.Id;
        cas1.IMEINumber__c = 'xxxxxx1';

        cas1.ContactId = [select Id from Contact where AccountId =: acc.Id limit 1 ].Id;
        insert cas1;
        System.Test.startTest();


        WorkOrder__c wo1=new WorkOrder__c();
        wo1.Case__c=cas1.Id;
        wo1.Account__c=acc.Id;
        wo1.Status__c= '处理中';
        wo1.IMEINumber__c = 'x0003';
        wo1.ManufactureInfo__c=p1.Id;
        wo1.recordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('NonAgentInstallationOrder','WorkOrder__c');
        insert wo1;

        wo1.Status__c = '完成';
        update wo1;


        /*WorkOrderProduct__c w1=new WorkOrderProduct__c();
        w1.WorkOrder__c=wo.Id;
        w1.Product__c=p1.Id;
        insert w1;
        WorkOrderProduct__c w2=new WorkOrderProduct__c();
        w2.WorkOrder__c=wo.Id;
        w2.Product__c=p2.Id;
        insert w2;*/
        WorkOrderProduct__c w3=new WorkOrderProduct__c();
        w3.WorkOrder__c=wo.Id;
        w3.Product__c=p4.Id;
        insert w3;


        Equipment__c eq=new Equipment__c();
        //eq.Name='testYQ';
        eq.Account__c=acc.Id;
        eq.Product__c=p1.Id;
        eq.IMEINumber__c = 'x0004';
        insert eq;
        System.debug('++++++++++');

        Equipment__c eeq2 = [select Id from Equipment__c where IMEINumber__c = 'x0003' limit 1 ];
        eeq2.Instrument__c = eq.Id;
        update eeq2;
        


        Return_Machine__c rmi=new Return_Machine__c();
        rmi.instrument__c=eq.Id;
        rmi.Status__c='申请中';
        
        insert rmi;
        rmi.Status__c='已批准';
        update rmi;

        wo.Status__c='完成';
        update wo;
        System.debug('------>');

          eq.type__c ='退机';
        update  eq;

       
        
        
        //wo3.Status__c='完成';
        //update wo3;


        /*
        Supplies__c ss=new Supplies__c();
        //ss.Name='HCtest';
        ss.Name='testYQ';
        ss.Account__c=acc.Id;
        ss.Product__c=p3.Id;
        insert ss;
        */
        /*
        Creditchange__c ccg=new Creditchange__c();
        ccg.Account__c=acc.Id;
        ccg.CreditLimit__c=1;
        ccg.stage__c='草稿';
        ccg.CreditDeadline__c='4';
        insert ccg;
        
        ccg.stage__c='审批通过';
        update ccg;
        */
    }
    
}